
package BattleGround

import(
	"fmt"
	"sanguo/node/table/excel"
	"sanguo/node/table/excel/table"
	"strconv"

	"github.com/tealeg/xlsx"
)

func readInt32(v string) int32 {
	return int32(readFloat(v))
}

func readFloat(v string) float64 {
	vv,err := strconv.ParseFloat(v,64)
	if nil != err {
		panic(err)
	}
	return vv
}

func readBool(str string) bool {
    b, err := strconv.ParseBool(str)
	if nil != err {
		panic(err)
	}
    return b
}

func readStr(v string) string {
	return fmt.Sprintf("%s",v)
}

type Explore_ struct {
	Times int32
}

type BuffID_ struct {
	BuffId int32
}

type BattleGround struct {
	ID int32
	UnLockLevel int32
	Count int32
	ThroughDropPool int32
	ExploreUnlockLevel int32
	Explore []Explore_
	DiamondCost int32
	BoosRangeLeft int32
	BoosRangeRight int32
	BoosMaxCount int32
	EliteRangeLeft int32
	EliteRangeRight int32
	EliteMaxCount int32
	GeneralRangeLeft int32
	GeneralRangeRight int32
	BuffID []BuffID_
}

func readExplore(value string) []Explore_ {

	Explore := make([]Explore_,0)
	r := excel.Split(value,",;")

	for _,v := range(r) {
		Explore_ := Explore_{}

		Explore_.Times = readInt32(v[0])
	
		Explore = append(Explore,Explore_)
	}	 	
	return Explore
}

func readBuffID(value string) []BuffID_ {

	BuffID := make([]BuffID_,0)
	r := excel.Split(value,",;")

	for _,v := range(r) {
		BuffID_ := BuffID_{}

		BuffID_.BuffId = readInt32(v[0])
	
		BuffID = append(BuffID,BuffID_)
	}	 	
	return BuffID
}

type Table struct {
	IDMap map[int32]*BattleGround
	tmpIDMap map[int32]*BattleGround
	xlsxName string
}

var Table_ Table

func getIdxByName(names *xlsx.Row, name string) int {
	for i, v := range names.Cells {
		if v.String() == name {
			return i
		}
	}
	return -1
}

func init() {
	Table_.xlsxName = "BattleGround.xlsx"
	Table_.IDMap = make(map[int32]*BattleGround)
	Table_.tmpIDMap = make(map[int32]*BattleGround)

	table.AddTable(&Table_)
}


func (this *Table) Load(path string) {

	excel.EmptyFunc()

    xlFile, err := xlsx.OpenFile(path + this.xlsxName)
    if err != nil {
		panic(err)
    }

    sheet := xlFile.Sheets[0]
    names := sheet.Rows[1]
    rows  := sheet.Rows[3:]

    for _,row := range(rows) {
    	if len(row.Cells) > 0 && row.Cells[0].String() != "" {
    		ele := &BattleGround{}
    	
		ele.ID = readInt32(row.Cells[getIdxByName(names, "ID")].String())
		this.tmpIDMap[ele.ID] = ele
		ele.UnLockLevel = readInt32(row.Cells[getIdxByName(names, "UnLockLevel")].String())
		ele.Count = readInt32(row.Cells[getIdxByName(names, "Count")].String())
		ele.ThroughDropPool = readInt32(row.Cells[getIdxByName(names, "ThroughDropPool")].String())
		ele.ExploreUnlockLevel = readInt32(row.Cells[getIdxByName(names, "ExploreUnlockLevel")].String())
		ele.Explore = readExplore(row.Cells[getIdxByName(names, "Explore")].String())
		ele.DiamondCost = readInt32(row.Cells[getIdxByName(names, "DiamondCost")].String())
		ele.BoosRangeLeft = readInt32(row.Cells[getIdxByName(names, "BoosRangeLeft")].String())
		ele.BoosRangeRight = readInt32(row.Cells[getIdxByName(names, "BoosRangeRight")].String())
		ele.BoosMaxCount = readInt32(row.Cells[getIdxByName(names, "BoosMaxCount")].String())
		ele.EliteRangeLeft = readInt32(row.Cells[getIdxByName(names, "EliteRangeLeft")].String())
		ele.EliteRangeRight = readInt32(row.Cells[getIdxByName(names, "EliteRangeRight")].String())
		ele.EliteMaxCount = readInt32(row.Cells[getIdxByName(names, "EliteMaxCount")].String())
		ele.GeneralRangeLeft = readInt32(row.Cells[getIdxByName(names, "GeneralRangeLeft")].String())
		ele.GeneralRangeRight = readInt32(row.Cells[getIdxByName(names, "GeneralRangeRight")].String())
		ele.BuffID = readBuffID(row.Cells[getIdxByName(names, "BuffID")].String())

		}
    }

    this.IDMap = this.tmpIDMap
 
}

